<?php

namespace app\admin\controller;


use app\admin\model\SystemAdmin;
use app\admin\model\SystemQuick;
use app\admin\service\SystemInfoService;
use app\common\controller\AdminController;
use think\App;

class Index extends AdminController
{

    /**
     * 后台主页
     * @return string
     * @throws \Exception
     */
    public function index()
    {
        $this->assign('admin', session('admin'));
        return $this->fetch();
    }

    /**
     * 后台欢迎页
     * @return string
     * @throws \Exception
     */
    public function welcome()
    {
        $quicks = SystemQuick::field('id,title,icon,href')
            ->where(['status' => 1])
            ->order('sort', 'desc')
            ->limit(8)
            ->select();
        // 订单数据
        $orderModel = new \app\admin\model\mall\Order();
        $goodsModel = new \app\admin\model\mall\Goods();
        $identityModel = new \app\admin\model\company\Identity();
        $sum_order_goods = $orderModel->where('pay_status',1)->where('refund_status',0)->sum('goods_num') ??0; //总商品数量 统计
        // $sum_order_profit = 0.00; //利润 统计
        // if (session('admin')['auth_ids'] == 7) { //供应商
        //     // 供应商 利润=售价-供货价(原进价)-运营物流费 
        //     // $sum_order_profit = $orderModel->where('pay_status',1)->where('refund_status',0)->sum('supplier_profit');
        //     // 供应商 利润=(供货价(原进价)+运营物流费)*袋数
        //     $goodsData = $goodsModel->where('is_default',1)->find();
        //     $sum_order_profit = number_format((($goodsData->purchase_price+$goodsData->shipping_cost) *$sum_order_goods),2,".","");
        // } else if (session('admin')['auth_ids'] == 8) { //经销商
        //     // 经销商 利润=售价-供货价(原进价)-运营物流费-(业务员提点|门店提点)
        //     $sum_order_profit = $orderModel->where('pay_status',1)->where('refund_status',0)->sum('dealer_profit');
        // }
        // 统计
        $data = [
            'sum_salesman' => $identityModel->where('type',3)->count(), //业务员 统计
            'sum_shop' => $identityModel->where('type',4)->count(), //门店 统计
            'sum_user' => $identityModel->where('type',5)->count(), //用户 统计
            'sum_order' => $orderModel->where('pay_status',1)->count(), //订单 统计
            'sum_order_price' => number_format(($orderModel->where('pay_status',1)->where('refund_status',0)->sum('ok_amount')),2,".","") ??0.00, //总金额 统计
            // 'sum_order_goods' => $orderModel->where('pay_status',1)->sum('goods_num') + $orderModel->where('pay_status',1)->sum('gift_num'), //总商品数量 统计
            'sum_order_goods' => $sum_order_goods, //总商品数量 统计
            'sum_order_deal' => $orderModel->where('pay_status',1)->where('refund_status',0)->count(), // 订单成交 统计
            'sum_order_refund' => $orderModel->where('pay_status',1)->where('refund_status',1)->count(), // 订单退款 统计
            // 'sum_order_profit' => number_format(($sum_order_profit),2,".",""), // 利润 统计
        ];
        // print_r($data); exit;
        $this->assign('data', $data);
        $this->assign('quicks', $quicks);
        $this->assign('systemInfo', SystemInfoService::getInstance()->getAll());
        $this->layoutBgColor();
        return $this->fetch();
    }

    /**
     * 修改管理员信息
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editAdmin()
    {
        $id = session('admin.id');
        $row = (new SystemAdmin())
            ->withoutField('password')
            ->find($id);
        empty($row) && $this->error('用户信息不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row
                    ->allowField(['head_img', 'phone', 'remark', 'update_time'])
                    ->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->layoutBgColor();
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * 修改密码
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editPassword()
    {
        $id = session('admin.id');
        $row = (new SystemAdmin())
            ->withoutField('password')
            ->find($id);
        if (!$row) {
            $this->error('用户信息不存在');
        }
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'password|登录密码'       => 'require',
                'password_again|确认密码' => 'require',
            ];
            $this->validate($post, $rule);
            if ($post['password'] != $post['password_again']) {
                $this->error('两次密码输入不一致');
            }

            try {
                $save = $row->save([
                    'password' => password($post['password']),
                ]);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $this->layoutBgColor();
        $this->assign('row', $row);
        return $this->fetch();
    }

}
